package cn.sieis.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.sieis.pojo.Contest;
import cn.sieis.pojo.Project;
import cn.sieis.pojo.School;
import cn.sieis.pojo.Student;
import cn.sieis.service.contest.ContestService;
import cn.sieis.service.project.ProjectService;
import cn.sieis.service.school.SchoolService;
import cn.sieis.service.student.StudentService;
import cn.sieis.tools.Constants;
import cn.sieis.tools.PageSupport;

@Controller
@RequestMapping("/back/student")
public class StudentController {

	@Resource
	private StudentService studentService;

	@Resource
	private SchoolService schoolService;

	@Resource
	private ProjectService projectService;

	@Resource
	private ContestService contestService;

	@RequestMapping(value = "/list.html")
	public String getStudentList(Model model, @RequestParam(value = "queryStuName", required = false) String queryStuName, @RequestParam(value = "queryStuSchool", required = false) String queryStuSchool, @RequestParam(value = "queryStuProject", required = false) String queryStuProject, @RequestParam(value = "queryStuContest", required = false) String queryStuContest, @RequestParam(value = "pageIndex", required = false) String pageIndex) {
		Integer _queryStuSchool = null;
		Integer _queryStuProject = null;
		Integer _queryStuContest = null;
		List<Student> studentList = null;
		List<School> schoolList = null;
		List<Project> projectList = null;
		List<Contest> contestList = null;
		int pageSize = Constants.pageSize;
		int currentPageNo = 1;

		if (queryStuName == null) {
			queryStuName = "";
		}
		if (queryStuSchool != null && !queryStuSchool.equals("")) {
			_queryStuSchool = Integer.parseInt(queryStuSchool);
		}
		if (queryStuProject != null && !queryStuProject.equals("")) {
			_queryStuProject = Integer.parseInt(queryStuProject);
		}
		if (queryStuContest != null && !queryStuContest.equals("")) {
			_queryStuContest = Integer.parseInt(queryStuContest);
		}

		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (NumberFormatException e) {
				return "redirect:/syserror.html";
			}
		}

		int totalCount = 0;
		try {
			totalCount = studentService.getStudentCount(queryStuName, _queryStuSchool, _queryStuProject, _queryStuContest);
		} catch (Exception e) {
			e.printStackTrace();
		}
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);
		int totalPageCount = pages.getTotalPageCount();
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}
		try {
			studentList = studentService.getStudentList(queryStuName, _queryStuSchool, _queryStuProject, _queryStuContest, currentPageNo, pageSize);
			schoolList = schoolService.getSchoolList();
			projectList = projectService.getProProList();
			contestList = contestService.getConConList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("studentList", studentList);
		model.addAttribute("schoolList", schoolList);
		model.addAttribute("projectList", projectList);
		model.addAttribute("contestList", contestList);
		model.addAttribute("queryStuName", queryStuName);
		model.addAttribute("queryStuSchool", queryStuSchool);
		model.addAttribute("queryProSchool", queryStuProject);
		model.addAttribute("queryStuProject", queryStuProject);
		model.addAttribute("totalPageCount", totalPageCount);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("currentPageNo", currentPageNo);
		return "back/studentlist";
	}

	@RequestMapping(value = "/add.html", method = RequestMethod.GET)
	public String addStudent(@ModelAttribute("Student") Student student) {
		return "back/studentadd";
	}

	@RequestMapping(value = "/addsave.html", method = RequestMethod.POST)
	public String addStudentSave(Student student, HttpSession session, HttpServletRequest request) {
		boolean flag = true;
		if (flag) {
			try {
				if (studentService.addStudent(student)) {
					return "redirect:/back/student/list.html";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "back/studentadd";
	}

	@RequestMapping(value = "/modifystudent/{id}", method = RequestMethod.GET)
	public String getStudentById(@PathVariable String id, Model model, HttpServletRequest request) {
		Student student = new Student();
		try {
			student = studentService.getStudentById(Integer.parseInt(id));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute(student);
		return "back/studentmodify";
	}

	@RequestMapping(value = "/modifysave.html", method = RequestMethod.POST)
	public String modifyStudentSave(Student student, HttpSession session, HttpServletRequest request) {
		boolean flag = true;
		if (flag) {
			try {
				if (studentService.modify(student)) {
					return "redirect:/back/student/list.html";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "back/studentmodify";
	}

	@RequestMapping(value = "/delstudent.html", method = RequestMethod.GET)
	public String delStudent(@RequestParam("id") String id) {
		try {
			if (studentService.deleteStudentById(Integer.parseInt(id))) {
			} else {
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/back/student/list.html";
	}

	@RequestMapping(value = "/stulist.html")
	public String getStuList(Model model, @RequestParam(value = "queryStuName", required = false) String queryStuName, @RequestParam(value = "queryStuSchool", required = false) String queryStuSchool, @RequestParam(value = "queryStuProject", required = false) String queryStuProject, @RequestParam(value = "queryStuContest", required = false) String queryStuContest, @RequestParam(value = "pageIndex", required = false) String pageIndex) {
		Integer _queryStuSchool = null;
		Integer _queryStuProject = null;
		Integer _queryStuContest = null;
		List<Student> studentList = null;
		List<School> schoolList = null;
		List<Project> projectList = null;
		List<Contest> contestList = null;
		int pageSize = Constants.pageSize;
		int currentPageNo = 1;
		if (queryStuName == null) {
			queryStuName = "";
		}
		if (queryStuSchool != null && !queryStuSchool.equals("")) {
			_queryStuSchool = Integer.parseInt(queryStuSchool);
		}
		if (queryStuProject != null && !queryStuProject.equals("")) {
			_queryStuProject = Integer.parseInt(queryStuProject);
		}
		if (queryStuContest != null && !queryStuContest.equals("")) {
			_queryStuContest = Integer.parseInt(queryStuContest);
		}

		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (NumberFormatException e) {
				return "redirect:/syserror.html";
			}
		}

		int totalCount = 0;
		try {
			totalCount = studentService.getStudentCount(queryStuName, _queryStuSchool, _queryStuProject, _queryStuContest);
		} catch (Exception e) {
			e.printStackTrace();
		}
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);
		int totalPageCount = pages.getTotalPageCount();
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}
		try {
			studentList = studentService.getStuList(queryStuName, _queryStuSchool, _queryStuProject, _queryStuContest);
			schoolList = schoolService.getSchoolList();
			projectList = projectService.getProProList();
			contestList = contestService.getConConList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("studentList", studentList);
		model.addAttribute("schoolList", schoolList);
		model.addAttribute("projectList", projectList);
		model.addAttribute("contestList", contestList);
		model.addAttribute("queryStuName", queryStuName);
		model.addAttribute("queryStuSchool", queryStuSchool);
		model.addAttribute("queryProSchool", queryStuProject);
		model.addAttribute("queryStuProject", queryStuProject);
		model.addAttribute("totalPageCount", totalPageCount);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("currentPageNo", currentPageNo);
		return "back/studentlist";
	}

	@RequestMapping(value = "/viewstudent/{id}", method = RequestMethod.GET)
	public String getStudenttByIdView(@PathVariable String id, Model model, HttpServletRequest request) {
		Student student = new Student();
		try {
			student = studentService.getStudentById(Integer.parseInt(id));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute(student);
		return "back/studentview";
	}

}
